package com.ruoyi.web.controller.vantController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.BorrowMoneyen;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.customer.PayableInstalment;
import com.ruoyi.system.service.IBorrowMoneyenService;
import com.ruoyi.system.service.vantservice.customerervice.ApiLocalFabulousService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/system/moneyen")
public class ApiBorrowMoneyenController extends BaseController {

    @Autowired
    private IBorrowMoneyenService borrowMoneyenService;
    @Autowired
    private HttpServletRequest request;
    @Resource
    private ApiLocalFabulousService apiLocalFabulousService;
    @Autowired
    private TokenUtils tokenUtils;
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody BorrowMoneyen borrowMoneyen)
    {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);

        BorrowMoneyen borrowMoneyen1 = new BorrowMoneyen();
        borrowMoneyen1.setCustomerId(customerLogin.getCustomerId());
        List<BorrowMoneyen> borrowMoneyens = borrowMoneyenService.selectBorrowMoneyenList(borrowMoneyen1);

        if (borrowMoneyens.size() >=1){
            return AjaxResult.error("您已申请过");
        }else{
            borrowMoneyen.setCustomerId(customerLogin.getCustomerId());
            borrowMoneyen.setState("0");
            borrowMoneyen.setAddTime(new Date());
            return toAjax(borrowMoneyenService.insertBorrowMoneyen(borrowMoneyen));
        }

    }


    @PostMapping("/uploadVoucher")
    public Map<String, Object> uploadVoucher(@RequestParam("file") MultipartFile[] file) {
        Map<String, Object> resultMap = apiLocalFabulousService.uploadPingzheng(file);
        return resultMap;
    }


}
